<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * @author vostro
 *
 */
class WriteLog {

	private static $isWriteLog = true;

	public function __construct() {
		$CI =& get_instance();
		$CI->load->helper('file');
	}

	/**
	 * @author DatDM
	 * @param string $output => string need write log
	 * @param string $tag => DEBUG, ERROR, INFO
	 * @param string $filename => file name
	 */
	public function log($output, $tag = 'DEBUG', $filename = '') {
		if(self::$isWriteLog) {
			$filename = strlen(trim($filename)) == 0 ? time() . '.log.txt' : $filename;
			$took = round(time(), 3);

			// DEBUG - 2013-03-15 15:25:58 --> xyz
			$tag = "$tag - " . date('Y-m-d H:i:s');
			$output = "$tag --> $output";
			$output .= "===[took:{$took}]\n\n";
			if ( ! write_file(APPPATH  . "/logs/$filename", $output, 'a+')){
				log_message('debug','Unable to write log the file ' . APPPATH  . "/logs/$filename");
			}
		}
	}
}